import { Behaviour, core, Transform, getGameObjectById, SpriteRenderer } from "../gameengine";
import { missionManager } from "./MissionManager";
import { MissionStatus } from "./Mission";
import { user } from "../user";
import { MainRoleBehaviour } from "../mainRoleBehaviour";
import { platform } from "os";
import { music } from "../music/music";
import { plot } from "../plot/plot";


export class MissionSystem extends Behaviour {

    private duringTime: number = 0;

    protected onStart() {
        // 初始需要开始的任务自动开始
        for (let mission of missionManager.missions) {
            if (mission.status == MissionStatus.ACCEPTABLE) {
                if (mission.id == 1 || mission.id == 3) {
                    mission.accept();
                }

            }
        }
        // console.log(missionManager.missions);
    }

    protected onUpdate(advancedTime: number) {

        for (let mission of missionManager.missions) {

            //自动完成可提交任务
            if (mission.status == MissionStatus.CANSUBMIT) {
                if (mission)
                    mission.submit();
            }

            //根据情况开始进行任务
            if (mission.status == MissionStatus.ACCEPTABLE) {

                if (mission.id == 2) {    //自动添加金钥匙进背包
                    user.keys.push("goldenKey");
                    console.log(user.keys);
                    mission.accept();
                }
                if (mission.id == 6) {   //两个击杀必须要检测到场景中有这玩意儿才开始
                    // const fengziB = getGameObjectById("fengziB");
                    if (user.limitedTimeMission == 6) {
                        mission.accept();
                        console.log("找到疯子B");
                    }
                }
                if (mission.id == 7) {   //两个击杀必须要检测到场景中有这玩意儿才开始     
                    // const fengziA = getGameObjectById("fengziA");
                    if (user.limitedTimeMission == 7) {
                        mission.accept();
                        console.log("找到疯子A");
                    }
                }
                if (mission.id == 8) {    //检测在一楼走廊才开始任务
                    // const door_ward1 = getGameObjectById("door_ward1");
                    if (user.limitedTimeMission == 8) {
                        mission.accept();
                        console.log("找到大门脚印");
                    }
                }
                if (mission.id == 9) {    //检测到疯子B才开始任务
                    // const fengziB = getGameObjectById("fengziB");
                    if (user.limitedTimeMission == 6) {
                        mission.accept();
                    }
                }
                if (mission.toItem == "none") {
                    mission.accept();
                }

            }

            //限时任务
            if (mission.status == MissionStatus.DURING) {

                //打火机任务
                if (mission.id == 6) {
                    this.duringTime += advancedTime;

                    if (this.duringTime > 20000) {     //任务失败的情况
                        console.log("任务失败！KILL");
                        mission.status = MissionStatus.FAILED;
                    }

                    for (let useBagItem of user.useBagItems) {
                        if (useBagItem == "fire") {
                            mission.status = MissionStatus.FINISHED;
                            console.log("怪物不敢靠近你了");
                        }
                    }

                }

                //手枪任务
                if (mission.id == 7) {
                    this.duringTime += advancedTime;

                    if (this.duringTime > 20000) {     //任务失败的情况
                        console.log("任务失败！KILL");
                        mission.status = MissionStatus.FAILED;
                    }

                    for (let useBagItem of user.useBagItems) {
                        if (useBagItem == "gun") {
                            // music.shootPlay();
                            console.log("你杀死了怪物");
                            mission.status = MissionStatus.FINISHED;

                        }
                    }

                }
            }

            //任务失败触发疯子KILL结局
            if (mission.status == MissionStatus.FAILED) {
                mission.status = MissionStatus.UNACCEPTABLE;
                user.limitedTimeMission = 0;
                plot.end2Plot();
                user.firstInStartScene = true;
                this.displayObject.parent.removeChild(this.displayObject);

            }

            if (mission.status == MissionStatus.FINISHED) {
                //完成任务8触发大门KILL结局
                if (mission.id == 8) {
                    plot.end1Plot();
                    user.limitedTimeMission = 0;
                    user.firstInStartScene = true;
                    mission.status = MissionStatus.UNACCEPTABLE;
                    this.displayObject.parent.removeChild(this.displayObject);

                }
                //完成任务9触发成功结局
                if (mission.id == 9) {
                    plot.end3Plot();
                    user.limitedTimeMission = 0;
                    user.firstInStartScene = true;
                    mission.status = MissionStatus.UNACCEPTABLE;
                    this.displayObject.parent.removeChild(this.displayObject);

                }
            }

        }




    }

}
